package com.lbl.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbl.msg.Msg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1:获取请求发送的数据
        String username = request.getParameter("username");
        response.setContentType("text/html;charset=UTF-8");
        //2:判断用户名是否注册
        if("abc123".equals(username)){
            Msg msg = new Msg("用户名已经注册",0);
            //用户名已经注册  {"flag":false,"info":"用户名已经注册"};
//            String jsonStr = "{\"flag\":false,\"info\":\"用户名已经注册\"}";
            //响应回浏览器
            response.getWriter().write(new ObjectMapper().writeValueAsString(msg));
        }else{
            Msg msg = new Msg("可以注册",1);
            //用户名没有注册
//            String jsonStr = "{\"flag\":true,\"info\":\"可以注册\"}";
            //响应回浏览器
            response.getWriter().write(new ObjectMapper().writeValueAsString(msg));
        }

    }
}
